<template>
  <a-row :gutter="10">
    <a-col :md="12" :sm="24">
      <a-card :bordered="false">
        <div style="background: #fff; padding-left: 16px; height: 100%; margin-top: 5px">
          <div>
            <a-row>
              <a-col span="12">
                <j-search-select-tag v-model="companyid" placeholder="请选择公司"
                    dict="sys_depart where org_type='1',depart_name,id"></j-search-select-tag>
              </a-col>
              <a-col span="12">
                <a-button @click="onSearch">查询</a-button>
              </a-col>
            </a-row>
          </div>
         
          <!-- 树-->

          <template>
            <!--仓库列表-->
            <a-table
              bordered
              :rowSelection="{
            selectedRowKeys: selectedWarehouseRowKeys,
            onChange: onWarehouseSelectChange,
            'type': 'radio',
            columnWidth: 30,
          }"
          :data-source="warehousedataSource"
          :columns="warehousecolumns">
        </a-table>
          </template>
        </div>
      </a-card>
    </a-col>
    <a-col :md="12" :sm="24">
      <a-card :bordered="true">
        <a-button class="editable-add-btn" @click="handleAdd" style="margin-bottom: 8px">新增</a-button>
        <a-button class="editable-add-btn" @click="handleEdit" style="margin-bottom: 8px">编辑</a-button>
        <a-button class="editable-add-btn" @click="handleDelete" style="margin-bottom: 8px">删除</a-button>
        <a-table
          bordered
          :row-selection="{ selectedRowKeys: selectedRowKeys, selectedRows: selectedRowKeys, onChange: onSelectChange }"
          :data-source="dataSource"
          :columns="columns"
        >
        </a-table>
      </a-card>
    </a-col>
    <warehouselinkman-modal ref="modalForm" @ok="onWarehouseSelectChange"></warehouselinkman-modal>
  </a-row>
</template>
<script>
//仓库保管员定义
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { querywarehousebycompanyid } from '@/api/erp/commonapi'
import { getAction, postAction } from '@/api/manage'
import WarehouselinkmanModal from './modules/WarehouselinkmanModal'
export default {
  name: 'WarehouselinkmanList',
  mixins: [JeecgListMixin],
  components: {
    WarehouselinkmanModal
  },
  data() {
    return {
      columns: [
        {
          dataIndex: 'userid_dictText',
          title: '保管员名称',
          align: 'center'
        },
      ],
      warehousecolumns:[
        {
          dataIndex:'code',
          title:'仓库编号',
          align:'center'
        },
        {
          dataIndex:'name',
          title:'仓库名称',
          align:'center'
        }
      ],
      warehousedataSource:[],
      companyid:'',
      currentDeptId: '',
      iExpandedKeys: [],
      loading: false,
      autoExpandParent: true,
      currFlowId: '',
      currFlowName: '',
      disable: true,
      treeData: [],
      visible: false,
      objectTree: [],
      rightClickSelectedKey: '',
      hiding: true,
      model: {},
      dropTrigger: '',
      depart: {},
      selectedWarehouseRowKeys:[],
      selectedRowKeys: [],
      selectedRows: [],
      disableSubmit: false,
      checkedKeys: [],
      selectedKeys: [],
      dataSource: [],
      autoIncr: 1,
      currSelected: {},
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      graphDatasource: {
        nodes: [],
        edges: []
      },
      userIdentity: '',
      url: {
        queryById: '/base/scmobject/queryById',
        deleteUrl: '/base/scmprintdefinition/delete'
      }
    }
  },
  methods: {
    /**
     * 跳转打印格式设计界面进行设计
     */

    handleDesign() {
      //弹出编辑窗口进行编辑
      if (this.selectedRowKeys === null || this.selectedRowKeys.length === 0) {
        this.$message.warning('请选中行再进行操作!')
        return
      }
      if (this.selectedRowKeys.length > 1) {
        this.$message.warning('请选择一行数据')
        return
      }

      //TODO 将当前行的数据进行备份，将数据传递到打印格式页面
      //缓存当前页面数据
      localStorage.setItem('currentPrintDefid', this.selectedRows[0].id)
      this.$router.push({ path: '/printmodule/demo/custom/index' })
    },
    /**
     * 编辑
     */
    handleEdit() {
      //弹出编辑窗口进行编辑
      if (this.selectedRowKeys === null || this.selectedRowKeys.length === 0) {
        this.$message.warning('请选中行再进行操作!')
        return
      }
      this.$refs.modalForm.edit(this.selectedRows[0])
      this.$refs.modalForm.title = '编辑'
    },
    /**
     * 删除行
     */
    handleDelete() {
      //弹出编辑窗口进行编辑
      if (this.selectedRowKeys === null || this.selectedRowKeys.length === 0) {
        this.$message.warning('请选中行再进行操作!')
        return
      }
      let param = {
        id: this.selectedRows[0].id
      }
      deleteAction(this.url.deleteUrl, param).then(res => {
        if (res) {
          this.$message.success('删除成功')
          return
        }
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    /**
     * 卡片新增
     */
    handleAdd() {
      if (this.selectedWarehouseRowKeys === []) {
        this.$message.warning('请先选择仓库')
        return
      }
      localStorage.setItem('linkmanwarehouseid', this.selectedWarehouseRowKeys[0])
      localStorage.setItem("linkmancompanyid",this.companyid);
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
    },
    loadData() {
      this.refresh()
    },
    clearSelectedDepartKeys() {
      this.checkedKeys = []
      this.selectedKeys = []
      this.currentDeptId = ''
      this.$refs.DeptUserInfo.currentDeptId = ''
      this.$refs.DeptRoleInfo.currentDeptId = ''
    },
    loadTree() {
      var that = this
      let params={
        companyid:this.companyid
      }
      this.loading = true;
      querywarehousebycompanyid(params).then(res => {
        console.log(res);
        if (res.success && res.result) {
          let cur = res.result.records
          this.warehousedataSource = cur;
          this.loading = false
        }
      })
    },
    setThisExpandedKeys(node) {
      //只展开一级目录
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        //下方代码放开注释则默认展开所有节点

        for (let a = 0; a < node.children.length; a++) {
          this.setThisExpandedKeys(node.children[a])
        }
      }
    },
    refresh() {
      if(this.companyid === ''){
        return;
      }
      this.loading = true
      this.loadTree()
    },

    onSearch() {
      if(this.companyid=== '' || this.companyid === null || this.companyid === undefined ){
        this.$message.error("请先选择公司!");
        return;
      }
      this.loadTree()
    },
    onCheck(checkedKeys, e) {
      let record = e.node.dataRef
      // console.log('onCheck', checkedKeys, e);
      this.checkedKeys = []
      // if (e.checked === true) {
      this.currentDeptId = record.id
      this.checkedKeys.push(record.id)

      this.$refs.DeptBaseInfo.open(record)
      this.$refs.DeptUserInfo.open(record)
      this.$refs.DeptRoleInfo.open(record)
      // }
      // else {
      //   this.checkedKeys = [];
      //   this.$refs.DeptBaseInfo.clearForm();
      //   this.$refs.DeptUserInfo.clearList();
      // }

      this.hiding = false
      // this.checkedKeys = checkedKeys.checked
    },
    onWarehouseSelectChange(selectedKeys,e){
      //获取仓库保管员信息
      let params = {
          warehouseid : e[0].id
      }

      this.selectedWarehouseRowKeys=[];
      this.selectedWarehouseRowKeys.push(e[0].id);
      
      getAction("/base/warehouselinkman/list",params).then(res=>{
        if(res.success){
          this.dataSource = res.result.records;
        }
      })
    }
  }
}
</script>
